﻿using ChuanYe.CountyFireManagement.EF.MySQL;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace ChuanYe.CountyFireManagement.BLL
{
    public class LinchpinBLL
    {

        #region 构造单例
        private LinchpinBLL() { }
        private static LinchpinBLL _instance;
        public static LinchpinBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new LinchpinBLL());
            }
        }
        #endregion

        public Linchpin GetLinchpin(long linchpinId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                return ef.Linchpin.Where(p => p.LinchpinId == linchpinId).FirstOrDefault();
            }
        }

        public List<Linchpin> GetSubLinchpin(long linchpinId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                return ef.Linchpin.Where(p => p.ParentId == linchpinId).ToList();
            }

        }

        public List<Linchpin> GetLinchpin(List<long> linchpinIds)
        {

            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                return ef.Linchpin.Where(p => linchpinIds.Contains(p.LinchpinId)).OrderBy(o => o.LinchpinId).ToList();
            }
        }


        public Userareaprobability GetUserAreaProbability(int userId, int roleId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var linq = from a in ef.Userareaprobability select a;
                if (userId > 0)
                {
                    linq = linq.Where(p => p.UserId == userId);
                }
                if (roleId > 0)
                {
                    linq = linq.Where(p => p.RoleId == roleId);
                }
                return linq.OrderByDescending(o => o.AddTime).FirstOrDefault();
            }
        }
    }

}
